package com.xdap.keymen.service.punch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.xdap.keymen.entity.*;
import com.xdap.keymen.entity.dayang.DaYangOrderTitleEntity;
import com.xdap.motor.vo.ResponseVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class PunchDeptService extends PunchBaseService{
    public ResponseVo getDeptList(String upddate) {
        Gson gson = new GsonBuilder().create();
        Map<String, String> params=new HashMap<>();
        Map<String, String> headers=new HashMap<>();
        String benbu="wBEr3ujTQMOfasoamfQIrsznrtQ=";
        //每页条数
        int sz=500;
        //页面
        int pg=1;
            headers=new HashMap<>();
            headers.put("Authorization", "Bearer "+getPunchToken());
            String response = okHttpUtils.doGet(getPunchUrl().concat("/CRM/api/hr/Dept/"+upddate+"/"+sz+"/"+pg), params,headers);
            PunchResponse punchResponse = gson.fromJson(response, new TypeToken<PunchResponse>(){}.getType());
            List<PunchDeptResponseEntity> list = gson.fromJson(punchResponse.getResult(), new TypeToken<List<PunchDeptResponseEntity>>(){}.getType());
            List<DefanDepartmentEntity> listDept=new ArrayList<>();
            List<DefanDepartmentEntity> listDept2=new ArrayList<>();
            List<DefanDepartmentEntity> listDept3=new ArrayList<>();
            for(PunchDeptResponseEntity deptResponseEntity:list){
                //判断本部
                if(deptResponseEntity.getFid().equals(benbu)){
                    DefanDepartmentEntity benbuDept=new DefanDepartmentEntity();
                    benbuDept.setDepartmentCode(deptResponseEntity.getFid());
                    benbuDept.setDepartmentName(deptResponseEntity.getFname());
                    benbuDept.setDepartmentStatus("ENABLE");
                    listDept.add(benbuDept);
                }
                //二级
                if(deptResponseEntity.getFParentID().equals(benbu)){
                    DefanDepartmentEntity dept2=new DefanDepartmentEntity();
                    dept2.setParentCode(benbu);
                    dept2.setDepartmentCode(deptResponseEntity.getFid());
                    dept2.setDepartmentName(deptResponseEntity.getFname());
                    dept2.setDepartmentStatus("ENABLE");
                    listDept2.add(dept2);
                }
            }
            //保存一级部门
            saveDefanDept(listDept);
            //保存二级部门
            saveDefanDept(listDept2);
            for(PunchDeptResponseEntity deptResponseEntity:list){
                //判断是否是二级部门
                if(listDept2.stream().anyMatch(dept -> dept.getDepartmentCode().equals(deptResponseEntity.getFParentID()))){
                    DefanDepartmentEntity dept3=new DefanDepartmentEntity();
                    dept3.setParentCode(deptResponseEntity.getFParentID());
                    dept3.setDepartmentCode(deptResponseEntity.getFid());
                    dept3.setDepartmentName(deptResponseEntity.getFname());
                    dept3.setDepartmentStatus("ENABLE");
                    listDept3.add(dept3);
                }
            }
            //保存三级部门
            saveDefanDept(listDept3);

        return ResponseVo.ok().setData(response);
    }

    private void saveDefanDept(List<DefanDepartmentEntity> list){
        Map<String, Object> params=new HashMap<>();
        Gson gson = new GsonBuilder().create();
        params.put("apiCreateDepartmentDTOList",list);
        params.put("extendInfoVerify","false");
        Map<String, String> headers=new HashMap<>();
        headers.put("xdaptoken", this.getDefanUserToken());
        headers.put("Content-Type", "application/json; charset=utf-8");
        headers.put("xdaptenantid", "733682782865195009");
        headers.put("xdaptimestamp", String.valueOf(Instant.now().toEpochMilli()));
        String url="https://apaastest.punch.com.cn:8282/backend/xdap-app/department/api/create/department";
        //String response = okHttpUtils.doPost(url,params,headers);
        String strJson=gson.toJson(params);
        String response = okHttpUtils.doPostJson(url,strJson,headers, new HashMap<>());
        JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
    }

    /**
     * 根据部门编码获取得帆部门信息
     * @param deptCode
     * @return
     */
    public PunchXdapDepartmentsEntity getPunchXadpDept(String deptCode){
        String databaseName="xdap_app_"+runtimeAppContextService.getCurrentTenantId();
        PunchXdapDepartmentsEntity entity= runtimeDatasourceService.buildTenantMpaasQuery(databaseName)
                .like("department_code", deptCode)
                .doQueryFirst(PunchXdapDepartmentsEntity.class);
        return entity;
    }
}
